
Ext.define('proyectos.AtributoIForm', {
    extend: 'Ext.form.Panel',
    alias: 'widget.atributoIform',
    requires: ['Ext.form.field.Text'],
    initComponent: function(){
        Ext.apply(this, {
            activeRecord: null,
            iconCls: 'icon-atributos',
            frame: true,
            defaultType: 'textfield',
            bodyPadding: 20,
            fieldDefaults: {
                anchor: '100%',
            },
            items: [{
                fieldLabel: 'Id',
                name: 'id',
                type: 'int',
                itemId: 'idAtributo',
                hidden: true,
            },{
                fieldLabel: '<b>Nombre</b>',
                name: 'nombreAtributo',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
                itemId: 'nombre',
            }, {
                fieldLabel: '<b>Descripcion</b>',
                name: 'descripcion',
                itemId: 'descripcion',
                 fieldStyle: 'background-color: transparent; border: none; background-image: none;',
            },{
                fieldLabel: '<b>Valor</b>',
                name: 'valor',
                itemId: 'valor',
                fieldStyle: 'background-color: transparent; border: none; background-image: none;',
            }],
            dockedItems: [{
				xtype: 'toolbar',
				items: [
						{
							iconCls: 'icon-save',
							itemId: 'save',
							text: '<b>Guardar</b>',
							hidden: true,
							scope: this, 
							handler: this.onSave
						},{
							iconCls: 'icon-editar',
							itemId: 'editar',
							text: '<b>Editar</b>',
							hidden: true,
							scope: this, 
							handler: this.onEditar
						},{}]
				},{
				weight: 1,
                xtype: 'toolbar',
                dock: 'bottom',
                items: ['->',{
                    iconCls: 'icon-reset',
                    text: '<b>Reset</b>',
                    scope: this,
                    handler: this.onReset
                }]
            }]
        });
        this.callParent();
    },

    setActiveRecord: function(record){
        this.activeRecord = record;
        if (record) {

            this.down('#editar').show();
            this.down('#save').hide();
            this.down('#valor').setFieldStyle('background-color: transparent; border: none;');
            this.getForm().loadRecord(record);
        } else {

            this.down('#editar').hide();
            this.down('#save').hide();
            this.down('#nombre').setValue("");
            this.down('#descripcion').setValue("");
            this.down('#idAtributo').setValue("");
            this.down('#valor').setValue("");
            this.down('#valor').setFieldStyle('background-color: transparent; border: none;');
            
        }
    },

    onSave: function(){
        var form = this.getForm();
                    form.submit({                        
                        url: 'modificar_atributo_item',
                        params: {	submit: 'submit'	},
						method: "post",
                       success: function(form, action) {
								this.down('#nombre').setValue("");
								this.down('#descripcion').setValue("");
								this.down('#idAtributo').setValue("");
								//this.down('#tipo').setValue("");
								this.down('#valor').setValue("");
								this.fireEvent('salir');
							    Ext.Msg.show({  
									title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
									msg: 'Atributo modificado satisfactoriamente', //<- El mensaje  
									icon: 'ext-mb-ok',
								});
							this.fireEvent('salir');							                       
                        },
                        scope: this,                        
                        failure: function(form, action) {									
									Ext.Msg.show({  
										title: '<br><p align="center"><b>ALPHA MANAGER</b></p>', //<- el título del diálogo  
										msg: 'No se ha podido modificar el atributo', //<- El mensaje  
										icon: 'ext-mb-error',
									});
                        }
                    });
    },
    
    onEditar: function(){
		
		this.down('#editar').hide();		
		this.down('#save').show();
		this.down('#valor').setFieldStyle('background-color: #fff; border: 1px dotted;');
		
    },
    
    onReset: function(){
		//this.getForm().reset();
        this.setActiveRecord(null);        
        this.fireEvent('refresh');
    },

    
});
